// const { logger } = require('../utils');

class BaseService {
  async create(data, returning = ['id']) {
    try {
      return this.dbService.insert(data, returning);
    } catch (err) {
      // logger.error({
      //   Action: `${this.dbService.tableName}/create`,
      //   message: err.message,
      //   stack: err.stack,
      // });
      console.error(err);
      throw err;
    }
  }

  async find(userId) {
    try {
      return await this.dbService.getById(userId);
    } catch (err) {
      // logger.error({
      //   Action: `${this.dbService.tableName}/find`,
      //   message: err.message,
      //   stack: err.stack,
      // });
      console.error(err);
      throw err;
    }
  }

  async filter(where, offset, limit) {
    try {
      const result = this.dbService.knex(this.dbService.tableName).select()
        .where(where || {});
      if (offset === undefined && limit === undefined) {
        return result;
      }
      return result.limit(limit).offset(offset);
    } catch (err) {
      // logger.error({
      //   Action: `${this.dbService.tableName}/filter`,
      //   message: err.message,
      //   stack: err.stack,
      // });
      console.error(err);
      throw err;
    }
  }

  async update(id, data) {
    try {
      const result = await this.dbService.updateById(id, data);
      return result;
    } catch (err) {
      // logger.error({
      //   Action: `${this.dbService.tableName}/update`,
      //   message: err.message,
      //   stack: err.stack,
      // });
      console.error(err);
      throw err;
    }
  }

  async first(where) {
    try {
      return this.dbService.first(where);
    } catch (err) {
      // logger.error({
      //   Action: `${this.dbService.tableName}/first`,
      //   message: err.message,
      //   stack: err.stack,
      // });
      console.error(err);
      throw err;
    }
  }

  async delete(id) {
    try {
      return this.dbService.deleteById(id);
    } catch (err) {
      // logger.error({
      //   Action: `${this.dbService.tableName}/delete`,
      //   message: err.message,
      //   stack: err.stack,
      // });
      console.error(err);
      throw err;
    }
  }

  async deleteByWhere(where) {
    try {
      return this.dbService.deleteByWhere(where);
    } catch (err) {
      // logger.error({
      //   Action: `${this.dbService.tableName}/deleteByWhere`,
      //   message: err.message,
      //   stack: err.stack,
      // });
      console.error(err);
      throw err;
    }
  }

  async searchCount(where) {
    try {
      const result = this.dbService.knex(this.dbService.tableName)
        .where(where || {});
      const [{ count }] = await result.count({ count: '*' });
      return count;
    } catch (err) {
      // logger.error({
      //   Action: `${this.dbService.tableName}/searchCount`,
      //   message: err.message,
      //   stack: err.stack,
      // });
      console.error(err);
      throw err;
    }
  }

  async search({ where, offset, limit }) {
    try {
      const result = this.dbService.knex(this.dbService.tableName).select()
        .where(where || {});
      return result.limit(limit).offset(offset);
    } catch (err) {
      // logger.error({
      //   Action: `${this.dbService.tableName}/search`,
      //   message: err.message,
      //   stack: err.stack,
      // });
      console.error(err);
      throw err;
    }
  }
}

module.exports = BaseService;
